###########################################
## 2.createAppendixExhibits.R
## This script runs our analyses
## Last update: SR 15 Mar 2022
###########################################


    ## Create Appendix Table 1
        
        datasummary_balance(~strat,
                            data = demo_dat,
                            fmt = 1,
                            align = "llcccccc",
                            type = "categorical",
                            output = "output/tables/demographic_table_a1.tex",
                            latex_options = 'striped')
        
        
        
    ## Create All Appendix Figures
        
      ## Clean up regression output to only include randomized strike attributes
        
        keep <- c("Other Country, Multilateral", "Other Country, Unilateral", 
                  "Own Country, Multilateral", "Own Country, Unilateral")
        
        
        out_nostrat <- out_nostrat %>% filter(level %in% keep)
        out_cstrat <- out_cstrat %>% filter(level %in% keep)
        
      ## Appendix Figure 2 -- Country Stratified OLS on Legitimacy and Support
        
          out_nostrat %>%
            mutate(respondentCountry = "full") %>%
            dplyr::bind_rows(out_cstrat) %>%
            filter((outcome == "Legitimacy" | outcome == "Support")) %>%
            rename(`Strike Type` = level,
                   Mechanism = outcome) %>%
            mutate(Country = case_when(respondentCountry == "full" ~ "Full Sample",
                                       respondentCountry == "France" ~ "France",
                                       respondentCountry == "USA" ~ "United States")) %>%
            ggplot(aes(x = estimate, y = `Strike Type`,  shape = Mechanism)) +
            geom_vline(aes(xintercept = 0)) +
            geom_point(aes(color = Mechanism), size = 4, na.rm = TRUE, position = ggstance::position_dodgev(height = 1)) +
            geom_linerange(aes(xmin = lower, xmax = upper, color = Mechanism),  size = 1, alpha = 0.5, position = ggstance::position_dodgev(height = 1)) +
            ylab("") +
            xlab("") +
            facet_grid(cols = vars(Country), scales = "free_x") +
            theme_minimal() +
            scale_colour_wsj() +
            geom_stripped_rows(odd = "#33333333", even = "#00000000") +
            theme(axis.text.y = element_text(),
                  text = element_text(size = 25),
                  legend.position = 'bottom',
                  legend.title = element_blank()) 
          
            ggsave("output/figures/appendix_figure_2.pdf", width = 17, height = 11)
          

        
      ## Appendix Figure 3 -- Country Stratified OLS on Hypothesized Mechanisms
        
        

          out_nostrat %>%
            mutate(respondentCountry = "full") %>%
            dplyr::bind_rows(out_cstrat) %>%
            filter(!(outcome == "Legitimacy" | outcome == "Support")) %>%
            rename(`Strike Type` = level,
                   Mechanism = outcome) %>%
            mutate(Country = case_when(respondentCountry == "full" ~ "Full Sample",
                                       respondentCountry == "France" ~ "France",
                                       respondentCountry == "USA" ~ "United States")) %>%
            ggplot(aes(x = estimate, y = `Strike Type`,  shape = Mechanism)) +
            geom_vline(aes(xintercept = 0)) +
            geom_point(aes(color = Mechanism), size = 4, na.rm = TRUE, position = ggstance::position_dodgev(height = 1)) +
            geom_linerange(aes(xmin = lower, xmax = upper, color = Mechanism),  size = 1, alpha = 0.5, position = ggstance::position_dodgev(height = 1)) +
            ylab("") +
            xlab("") +
            facet_grid(cols = vars(Country), scales = "free_x") +
            theme_minimal() +
            scale_colour_wsj() +
            geom_stripped_rows(odd = "#33333333", even = "#00000000") +
            theme(axis.text.y = element_text(),
                  text = element_text(size = 25),
                  legend.position = 'bottom',
                  legend.title = element_blank()) 
          
          ggsave("output/figures/appendix_figure_3.pdf", width = 17, height = 11)
          
        


        
        
        
  